package com.cngphone.AnFetion;

import java.io.Serializable;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.cngphone.AnFetion.Fetion.SIPC.SIPCBase;
import com.cngphone.AnFetion.tools.LogUtil;
import com.cngphone.AnFetion.tools.StrUtil;

public interface MessageXml extends Serializable {
    boolean parseXml(String xml);

    public static final class AddBuddyApplicationMessage implements MessageXml {
        private static final long serialVersionUID = 2220507636888142443L;
        public String uri, desc, time;

        @Override
        public boolean parseXml(String xml) {
            Document dom = StrUtil.parseXmlDocument(xml);
            Element results = dom.getDocumentElement();
            if (results == null || !results.getNodeName().equals("events")) {
                return false;
            }
            Node node = results.getFirstChild();
            if (node == null || !node.getNodeName().equals("event")) {
                return false;
            }
            Node type = node.getAttributes().getNamedItem("type");
            if (type != null && "AddBuddyApplication".equals(type.getNodeValue())) {
                node = node.getFirstChild();
                if (node == null || !node.getNodeName().equals("application")) {
                    return false;
                }
                NamedNodeMap attr = node.getAttributes();
                String s;
                Node n = attr.getNamedItem("uri");
                if (n == null || (s = n.getNodeValue()).equals("")) return false;
                uri = s;
                n = attr.getNamedItem("desc");
                desc = n.getNodeValue();
                n = attr.getNamedItem("time");
                time = n.getNodeValue();
                return true;
            }
            return false;
        }

    }

    public static final class UpdateBuddyMessage implements MessageXml {
        private static final long serialVersionUID = 8694138598618791429L;

        private ArrayList<FetionFriend> buddies;

        public UpdateBuddyMessage(ArrayList<FetionFriend> buddies) {
            this.buddies = buddies;
        }

        @Override
        public boolean parseXml(String xml) {
            Document dom = StrUtil.parseXmlDocument(xml);
            Element results = dom.getDocumentElement();
            if (results == null || !results.getNodeName().equals("events")) {
                return false;
            }
            Node node = results.getFirstChild();
            if (node == null || !node.getNodeName().equals("event")) {
                return false;
            }
            Node type = node.getAttributes().getNamedItem("type");
            if (type != null && "UpdateBuddy".equals(type.getNodeValue())) {
                Node contacts = node.getFirstChild();
                if (contacts == null || !contacts.getNodeName().equals("contacts")) {
                    return false;
                }
                NodeList buddiesnodes = ((Element) contacts).getElementsByTagName("buddies");
                if (buddiesnodes.getLength() > 0) {
                    NodeList buddyNodes = buddiesnodes.item(0).getChildNodes();
                    int size = buddyNodes.getLength();
                    for (int j = 0; j < size; j++) {
                        Element e = (Element) buddyNodes.item(j);
                        if (e.getNodeName().equals("buddy")) {
                            String uri = e.getAttribute("uri");
                            if (uri == null || uri.equals("")) {
                                break;
                            }
                            FetionFriend friend = FetionService.getService().getFriendManager().getFriend(uri);
                            if (friend == null) {
                                LogUtil.w(Settings.TAG, "Not in the list:" + uri);
                                break;
                            }
                            if (e.getAttribute("local-name") != null) {
                                friend.setFriendName(e.getAttribute("local-name"));
                            }
                            if (e.getAttribute("relation-status") != null) {
                                friend.type = StrUtil.parseInt(e.getAttribute("relation-status"), 0);
                            }
                            if (e.getAttribute("online-notify") != null) {
                                friend.onlineNotify = StrUtil.parseInt(e.getAttribute("online-notify"), 0);
                            }
                            if (e.getAttribute("buddy-lists") != null) {
                                friend.group = StrUtil.parseInt(e.getAttribute("buddy-lists"), 0);
                            }
                        }
                    }
                    return true;
                }
            }
            return false;
        }

    }

    public static final class ServiceResultMessage implements MessageXml {
        private static final long serialVersionUID = 5836596793773537415L;

        private ArrayList<FetionFriend> buddies;

        public ServiceResultMessage(ArrayList<FetionFriend> buddies) {
            this.buddies = buddies;
        }

        @Override
        public boolean parseXml(String xml) {
            Document doc = StrUtil.parseXmlDocument(xml);
            Element results = doc.getDocumentElement();
            if (results == null || !results.getNodeName().equals("events")) {
                return false;
            }
            Node node = results.getFirstChild();
            if (node == null || !node.getNodeName().equals("event")) {
                return false;
            }
            if (node.getAttributes().getNamedItem("type") == null
                    || !"ServiceResult".equals(node.getAttributes().getNamedItem("type").getNodeValue())) {
                return false;
            }
            NodeList contact = results.getElementsByTagName("contact");
            int size = contact.getLength();
            for (int i = 0; i < size; i++) {
                Element c = (Element) contact.item(i);
                if (c.getAttributeNode("uri") != null) {
                    String uri = c.getAttributeNode("uri").getNodeValue();
                    FetionFriend ff = FetionService.getService().getFriendManager().getFriend(uri);
                    if (ff != null) {
                        NodeList extended = c.getElementsByTagName("extended");
                        if (extended.getLength() == 1) {
                            node = ((Element) extended.item(0)).getAttributeNode("score-level");
                            if (node != null) {
                                ff.score = StrUtil.parseInt(node.getNodeValue(), 0);
                            }
                        }
                        NodeList personal = c.getElementsByTagName("personal");
                        if (personal.getLength() == 1) {
                            NamedNodeMap nnm = personal.item(0).getAttributes();
                            node = nnm.getNamedItem("nickname");
                            if (node != null && !node.getNodeValue().equals("")) {
                                ff.setNickName(node.getNodeValue());
                            }
                            node = nnm.getNamedItem("impresa");
                            if (node != null && !node.getNodeValue().equals("")) {
                                ff.impresa = node.getNodeValue();
                            }
                            node = nnm.getNamedItem("portrait-crc");
                            if (node != null && !node.getNodeValue().equals("")) {
                                ff.portrait = StrUtil.parseLong(node.getNodeValue(), 0);
                            }
                            node = nnm.getNamedItem("portrait-crc");
                            if (node != null && !node.getNodeValue().equals("")) {
                                ff.portrait = StrUtil.parseLong(node.getNodeValue(), 0);
                            }
                            node = nnm.getNamedItem("mobile-no");
                            if (node != null && !node.getNodeValue().equals("")) {
                                ff.mobile_no = node.getNodeValue();
                            }
                        }
                    }
                }
            }
            return true;
        }

    }

}
